<!DOCTYPE HTML>
<html class="butler">
<head>
    <title>科寓共享家</title>
    <% include common/meta.ejs %>
    <% include common/public.ejs %>
    <!--添加预定按钮样式-->
    <style>
        .bookBtn{
            color: #FFFFFF;
            margin-right: -200px;
        }
        .confirmBtn{
            color: #FFFFFF;
            margin-right: -200px;
        }
    </style>
</head>
<body>
<div class="weui-pull-to-refresh-layer">
    <div class="pull-to-refresh-arrow"></div> <!-- 上下拉动的时候显示的箭头 -->
    <div class="pull-to-refresh-preloader"></div> <!-- 正在刷新的菊花 -->
    <div class="down">下拉刷新</div><!-- 下拉过程显示的文案 -->
    <div class="up">释放刷新</div><!-- 下拉超过50px显示的文案 -->
    <div class="refresh">正在刷新...</div><!-- 正在刷新时显示的文案 -->
</div>
<div class="weui-row weui-no-gutter">
    <div class="weui-col-33">
        <a href="butlerBook?orderStatus=IOS00"
           class="topNav <%= data.condition.properties.orderStatus=='IOS00'?'active':'' %>">
            待处理
        </a>
    </div>
    <div class="weui-col-33">

        <a href="butlerBook?orderStatus=IOS01"
           class="topNav <%= data.condition.properties.orderStatus=='IOS01'?'active':'' %>">
            已接收
        </a>
    </div>
    <div class="weui-col-33">
        <a href="butlerBook?orderStatus=IOS02"
           class="topNav <%= data.condition.properties.orderStatus=='IOS02'?'active':'' %>">
            已完成
        </a>
    </div>
</div>

<%
//let stateMap = {
//    'IOS00': '待处理',
//    'IOS01': '已确认',
//    'IOS02': '已完成',
//}
let stateMap = {};
let tempArray = data.inspectionOrderStatus;
for(let idx of tempArray.keys()){
    stateMap[tempArray[idx].code] = tempArray[idx].name;
}

let list = data.list;
%>
<div id="doing"></div>
<div class="weui-infinite-scroll">
    <div class="infinite-preloader"></div><!-- 菊花 -->
    正在加载... <!-- 文案，可以自行修改 -->
</div>



<div id="editPopup" class="weui-popup-container  popup-bottom">
    <div class="weui-popup-overlay"></div>
    <div class="weui-popup-modal">
        <div class="toolbar">
            <div class="toolbar-inner">
                <a href="javascript:void(0);" class="picker-button" id="saveButton">保存</a>
                <h1 class="title">预约单</h1>
            </div>
        </div>
        <div class="modal-content">
            <form id="editForm">
                <div class="weui_cells weui_cells_form">
                    <div class="weui_cell">
                        <div class="weui_cell_hd"><label class="weui_label">预计时间</label></div>
                        <div class="weui_cell_bd weui_cell_primary">
                            <input type="text" data-toggle='date' name="time" id="calendar"/>
                        </div>
                    </div>
                </div>
                <div class="weui_cells_title">备注</div>
                <div class="weui_cells weui_cells_form">
                    <div class="weui_cell">
                        <div class="weui_cell_bd weui_cell_primary">
                            <textarea class="weui_textarea" placeholder="备注..." name="remark"></textarea>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<% include common/footer.ejs %>
<script>
    let loadFlag = false
    let pageNumber = 1;
    let editId = '';
    $('.editIcon').click(function () {
        editId = $(this).data('id');
        Dolphin.form.empty($('#editForm'));
        $('#editPopup').popup();
    });

    let statusData = {
        properties: {}
    };

    statusData.properties.orderStatus = `<%= data.condition.properties.orderStatus %>`;

    function loadingData() {
        console.log(loadFlag);
        pageNumber = 1;
        statusData.properties.pageSize = 10;
        statusData.properties.pageNumber = 0;
        console.log(statusData);
        if (!loadFlag) {
            loadFlag = true;
            Dolphin.ajax({
                url: CTX + '/api/00923c89a610415d8e70b1b4f2380e75',
                type: 'post',
                data: Dolphin.json2string(statusData),
                forceUrl: 'true',
                onSuccess: function (data) {
                    loadFlag = false;
                    pageNumber++;
                    let list = data.rows;
                    let contractId, con = '', con2 = '';
                    let state;
                    console.log(list.length);
                    if (list.length == 0) {
                        $('.weui-infinite-scroll').css('display', 'none');
                        con = ` <br>
                    <div style="text-align: center">
                         <i class="weui_icon_success_circle"></i>到底啦~</div>`
                        $('#doing').append(con);
                        $(document.body).destroyInfinite()
                        return;
                    }
                    for (let i = 0; i < list.length; i++) {

                        if (list[i].orderStatus == 'IOS00') {
                            state = '待处理';
                            con = con +
                                ` <div class="block">

    <h3>
        <span class="stateIcon"></span>
        状态：${state}
    </h3>
 <img src="${list[i].house.primaryImage }" class="houseImg" />
    <p>
        <span class="label">预约时间</span>
        <span>${list[i].checkRoomDate }</span>
    </p>
    <p>
        <span class="label">预约地址</span>
        <span>${list[i].house.myAddress}</span>
    </p>
    <p>
        <span class="label">预定房型</span>
        <span>${list[i].house.houseTypeName}</span>
    </p>
    <hr />
  <!--  <button class="editIcon" data-id="${list[i].id}"  data-hid="${list[i].house.id}"></button>-->
    <p>
        <span class="label">联系人</span>
        <span>${list[i].contactName }</span>
    </p>
    <p>
        <span class="label">联系电话</span>
        <span>${list[i].contactMobile}</span>
        <a class="telIcon" href="tel:${list[i].contactMobile}"></a>
    </p>
    <p>
        <span class="label">备注</span>
        <span>${list[i].remark==null?'暂无备注信息':list[i].remark }</span>
    </p>
    <p class="textCenter bookBtn">
        <a href="javascript:;" class="weui_btn weui_btn_mini weui_btn_primary" data-id="${list[i].id}">确认预约</a>
    </p></div>`
                        }
                        else if (list[i].orderStatus == 'IOS01') {
                            state = '已接收';
                            con2 = con2 +
                                ` <div class="block">
     <h3>
        <span class="stateIcon"></span>
        状态：${state}
    </h3>
 <img src="${list[i].house.primaryImage }" class="houseImg" />
    <p>
        <span class="label">预约时间</span>
        <span>${list[i].checkRoomDate }</span>
    </p>
    <p>
        <span class="label">预约地址</span>
        <span>${list[i].house.myAddress==null?'空':list[i].house.myAddress}</span>
    </p>
    <p>
        <span class="label">预约房型</span>
        <span>${list[i].house.houseTypeName }</span>
    </p>
    <hr />
  <!--  <button class="editIcon" data-id="${list[i].id}"  data-hid="${list[i].house.id}"></button>-->
    <p>
        <span class="label">联系人</span>
        <span>${list[i].contactName }</span>
    </p>
    <p>
        <span class="label">联系电话</span>
        <span>${list[i].contactMobile}</span>
        <a class="telIcon" href="tel:${list[i].contactMobile}"></a>
    </p>
    <p>
        <span class="label">备注</span>
        <span>${list[i].remark==null?'暂无备注信息':list[i].remark }</span>
    </p>
 <p class="textCenter confirmBtn">
        <a href="javascript:;" class="weui_btn weui_btn_mini weui_btn_primary" data-id="${list[i].id}">确认看房</a>
    </p></div> `
                        }
                        else if (list[i].orderStatus == 'IOS02') {

                            state = '已完成';
                            con2 = con2 +
                                ` <div class="block">
     <h3>
        <span class="stateIcon"></span>
        状态：${state}
    </h3>
 <img src="${list[i].house.primaryImage }" class="houseImg" />
    <p>
        <span class="label">预约时间</span>
        <span>${list[i].checkRoomDate }</span>
    </p>
    <p>
        <span class="label">预约地址</span>
        <span>${list[i].house.myAddress==null?'空':list[i].house.myAddress}</span>
    </p>
    <p>
        <span class="label">预约房型</span>
        <span>${list[i].house.houseTypeName}</span>
    </p>
    <hr />
  <!--  <button class="editIcon" data-id="${list[i].id}"  data-hid="${list[i].house.id}"></button>-->
    <p>
        <span class="label">联系人</span>
        <span>${list[i].contactName }</span>
    </p>
    <p>
        <span class="label">联系电话</span>
        <span>${list[i].contactMobile}</span>
        <a class="telIcon" href="tel:${list[i].contactMobile}"></a>
    </p>
    <p>
        <span class="label">备注</span>
        <span>${list[i].remark==null?'暂无备注信息':list[i].remark }</span>
    </p>
 </div> `;

                        }
                    }
                    ;
                    if (statusData.properties.orderStatus == 'IOS00') {
                        $('#doing').html(con);
                        $(document.body).pullToRefreshDone();
                    } else {
                        $('#doing').html(con2);
                        $(document.body).pullToRefreshDone();
                    }
                    ;
                    $('.bookBtn a').click(function () {
                        id = $(this).data('id');
                        console.log(id);
                        Dolphin.ajax({
                            url: CTX + '/api/0e868f2e51a44005950df4b810771393@id=' + id,
                            type: Dolphin.requestMethod.PUT,
                            forceUrl: 'true',
                            onSuccess: function (reData) {
                                var checkRoomDate = reData.value.checkRoomDate;
                                var managerName = reData.value.properties.managerName;
                                var managerPhone = reData.value.properties.managerPhone;
                                var userId = reData.value.userId;
                                Dolphin.ajax({
                                    url: CTX+'/api/5b90151b418f4ceb9469a6b265b0d617@id='+userId,
                                    type: Dolphin.requestMethod.GET,
                                    forceUrl:'true',
                                    onSuccess: function(reData){
                                        var openid = reData.value.weChat;
                                        var userName = reData.value.name;
                                        var userPhone = reData.value.phone;
                                        //管理员确认预约看房后微信消息给客户
                                        var msgData = {
                                            "url" : "http://"+Dolphin.wechatUrl+"/yams/view/order?openId="+openid,
                                            "openid" : openid,
                                            "first" : "尊敬的用户,您好!",
                                            "keyword1" : checkRoomDate,
                                            "keyword2" : userPhone,
                                            "keyword3" : userName,
                                            "remark" : "您的预约看房请求，已被管理员("+managerName+","+managerPhone+")受理，请按时赴约，谢谢！"
                                        }
                                        Dolphin.ajax({
                                            url: '<%= global.config.contextPath == '/' ? '' : global.config.contextPath %>/wexin/sendWXTemplateMsg',
                                            type: Dolphin.requestMethod.POST,
                                            data: Dolphin.json2string(msgData),
                                            forceUrl: true,
                                            onSuccess: function (reData) {
                                                $.toast("确认成功", function () {
                                                    location.reload();
                                                });
                                            }
                                        });
                                    }
                                });


                            }
                        });

                    });


                    $('.confirmBtn a').click(function () {
                        id = $(this).data('id');
                        console.log(id);
                        Dolphin.ajax({
                            url: CTX + '/api/0e868f2e51a44005950df4b810771393@id=' + id,
                            type: Dolphin.requestMethod.PUT,
                            forceUrl: 'true',
                            onSuccess: function (reData) {
                                var checkRoomDate = reData.value.checkRoomDate;
                                var managerName = reData.value.properties.managerName;
                                var managerPhone = reData.value.properties.managerPhone;
                                var userId = reData.value.userId;
                                Dolphin.ajax({
                                    url: CTX+'/api/5b90151b418f4ceb9469a6b265b0d617@id='+userId,
                                    type: Dolphin.requestMethod.GET,
                                    forceUrl:'true',
                                    onSuccess: function(reData){
                                        var openid = reData.value.weChat;
                                        var userName = reData.value.name;
                                        var userPhone = reData.value.phone;
                                        //管理员确认预约看房后微信消息给客户
                                        var msgData = {
                                            "url" : "http://"+Dolphin.wechatUrl+"/yams/view/order?openId="+openid,
                                            "openid" : openid,
                                            "first" : "尊敬的用户,您好!",
                                            "keyword1" : checkRoomDate,
                                            "keyword2" : userPhone,
                                            "keyword3" : userName,
                                            "remark" : "您的看房，已被管理员("+managerName+","+managerPhone+")受理，可以来入住，谢谢！"
                                        }
                                        Dolphin.ajax({
                                            url: '<%= global.config.contextPath == '/' ? '' : global.config.contextPath %>/wexin/sendWXTemplateMsg',
                                            type: Dolphin.requestMethod.POST,
                                            data: Dolphin.json2string(msgData),
                                            forceUrl: true,
                                            onSuccess: function (reData) {
                                                $.toast("确认成功", function () {
                                                    location.reload();
                                                });
                                            }
                                        });
                                    }
                                });


                            }
                        });

                    });
                }
            })
        }

    };

    function reloadingData() {
        $('.weui-infinite-scroll').css('display', 'block');
        statusData.properties.pageSize = 10;
        statusData.properties.pageNumber = pageNumber;
        if (!loadFlag) {
            loadFlag = true;
            Dolphin.ajax({
                url: CTX + '/api/00923c89a610415d8e70b1b4f2380e75',
                type: 'post',
                data: Dolphin.json2string(statusData),
                forceUrl: 'true',
                onSuccess: function (data) {
                    loadFlag = false;
                    pageNumber++;
                    console.log(pageNumber);
                    let list = data.rows;
                    let contractId, con = '', con2 = '';
                    let state;
                    if (list.length == 0) {
                        $('.weui-infinite-scroll').css('display', 'none');
                        con = ` <br>
                    <div style="text-align: center">
                         <i class="weui_icon_success_circle"></i>到底啦~</div>`
                        $('#doing').append(con);
                        $(document.body).destroyInfinite()
                        return;
                    }

                    for (let i = 0; i < list.length; i++) {

                        if (list[i].orderStatus == 'IOS00') {
                            state = '待处理'
                            con = con +
                                ` <div class="block">

    <h3>
        <span class="stateIcon"></span>
        状态：${state}
    </h3>
 <img src="${list[i].house.primaryImage }" class="houseImg" />
    <p>
        <span class="label">预约时间</span>
        <span>${list[i].checkRoomDate }</span>
    </p>
    <p>
        <span class="label">预约地址</span>
        <span>${list[i].myAddress}</span>
    </p>
    <p>
        <span class="label">报修房型</span>
        <span>${list[i].houseTypeName }</span>
    </p>
    <hr />
  <!--  <button class="editIcon" data-id="${list[i].id}"  data-hid="${list[i].house.id}"></button>-->
    <p>
        <span class="label">联系人</span>
        <span>${list[i].contactName }</span>
    </p>
    <p>
        <span class="label">联系电话</span>
        <span>${list[i].contactMobile}</span>
        <a class="telIcon" href="tel:${list[i].contactMobile}"></a>
    </p>
    <p>
        <span class="label">备注</span>
        <pre>${list[i].remark }</pre>
    </p>
    <p class="textCenter bookBtn" >
        <a href="javascript:;" class="weui_btn weui_btn_mini weui_btn_primary" data-id="${list[i].id}">确认预约</a>
    </p></div>`
                        }

                        else if (list[i].orderStatus == 'IOS01') {
                            state = '已接收'
                            con2 = con2 +
                                ` <div class="block">
     <h3>
        <span class="stateIcon"></span>
        状态：${state}
    </h3>
 <img src="${list[i].house.primaryImage }" class="houseImg" />
    <p>
        <span class="label">预约时间</span>
        <span>${list[i].checkRoomDate }</span>
    </p>
    <p>
        <span class="label">预约地址</span>
        <span>${list[i].myAddress}</span>
    </p>
    <p>
        <span class="label">报修房型</span>
        <span>${list[i].houseTypeName }</span>
    </p>
    <hr />
  <!--  <button class="editIcon" data-id="${list[i].id}"  data-hid="${list[i].house.id}"></button>-->
    <p>
        <span class="label">联系人</span>
        <span>${list[i].contactName }</span>
    </p>
    <p>
        <span class="label">联系电话</span>
        <span>${list[i].contactMobile}</span>
        <a class="telIcon" href="tel:${list[i].contactMobile}"></a>
    </p>
    <p>
        <span class="label">备注</span>
        <pre>${list[i].remark }</pre>
    </p>
 </div> `
                        }
                        else if (list[i].orderStatus == 'IOS02') {

                            state = '已完成';
                            con2 = con2 +
                                ` <div class="block">
     <h3>
        <span class="stateIcon"></span>
        状态：${state}
    </h3>
 <img src="${list[i].house.primaryImage }" class="houseImg" />
    <p>
        <span class="label">预约时间</span>
        <span>${list[i].checkRoomDate }</span>
    </p>
    <p>
        <span class="label">预约地址</span>
        <span>${list[i].myAddress}</span>
    </p>
    <p>
        <span class="label">报修房型</span>
        <span>${list[i].houseTypeName }</span>
    </p>
    <hr />
  <!--  <button class="editIcon" data-id="${list[i].id}"  data-hid="${list[i].house.id}"></button>-->
    <p>
        <span class="label">联系人</span>
        <span>${list[i].contactName }</span>
    </p>
    <p>
        <span class="label">联系电话</span>
        <span>${list[i].contactMobile}</span>
        <a class="telIcon" href="tel:${list[i].contactMobile}"></a>
    </p>
    <p>
        <span class="label">备注</span>
        <pre>${list[i].remark }</pre>
    </p>
 </div> `;

                        }
                    }
                    ;
                    if (statusData.properties.orderStatus == 'IOS00') {
                        $('#doing').append(con);
                        $(document.body).pullToRefreshDone();
                    } else {
                        $('#doing').append(con2);
                        $(document.body).pullToRefreshDone();
                    }
                    ;
                    $('.bookBtn a').click(function () {
                        id = $(this).data('id');
                        console.log(id);
                        Dolphin.ajax({
                            url: CTX + '/api/0e868f2e51a44005950df4b810771393@id=' + id,
                            type: Dolphin.requestMethod.PUT,
                            onSuccess: function (reData) {
                                var checkRoomDate = reData.value.checkRoomDate;
                                var managerName = reData.value.properties.managerName;
                                var managerPhone = reData.value.properties.managerPhone;
                                var userId = reData.value.userId;
                                Dolphin.ajax({
                                    url: '/api/5b90151b418f4ceb9469a6b265b0d617@id='+userId,
                                    type: Dolphin.requestMethod.GET,
                                    forceUrl:'true',
                                    onSuccess: function(reData){
                                        var openid = reData.value.weChat;
                                        var userName = reData.value.name;
                                        var userPhone = reData.value.phone;
                                        //管理员确认预约看房后微信消息给客户
                                        var msgData = {
                                            "url" : "http://"+Dolphin.wechatUrl+"/yams/view/order?openId="+openid,
                                            "openid" : openid,
                                            "first" : "尊敬的用户,您好!",
                                            "keyword1" : checkRoomDate,
                                            "keyword2" : userPhone,
                                            "keyword3" : userName,
                                            "remark" : "您的预约看房请求，已被管理员("+managerName+","+managerPhone+")受理，请按时赴约，谢谢！"
                                        }
                                        Dolphin.ajax({
                                            url: '<%= global.config.contextPath == '/' ? '' : global.config.contextPath %>/wexin/sendWXTemplateMsg',
                                            type: Dolphin.requestMethod.POST,
                                            data: Dolphin.json2string(msgData),
                                            forceUrl: true,
                                            onSuccess: function (reData) {
                                                $.toast("确认成功", function () {
                                                    location.reload();
                                                });
                                            }
                                        });
                                    }
                                });
                            }
                        });

                    });
                }
            })
        }

    };

    $(document).ready(
        function () {
            loadingData();
        }
    );

    $(document.body).pullToRefresh();

    $(document.body).on("pull-to-refresh", function () {
        //do something
        loadingData();
        $(document.body).infinite(50);
    });

    $('.weui-infinite-scroll').css('display', 'none');

    $(document.body).infinite(20);

    var loading = false;  //状态标记

    $(document.body).infinite().on("infinite", function () {
        $('.weui-infinite-scroll').css('display', 'block');
        if (loading) return;
        loading = true;
        setTimeout(function () {
            if (!loadFlag) {
                reloadingData();
            }
            loading = false;

        }, 1500);   //模拟延迟
    });

    /*   $('#saveButton').click(function () {
           let data = Dolphin.form.getValue('editForm');
           let sendData={

           };
           sendData.id=editId;
           sendData.remark=data.remark;
           sendData.checkRoomDate =data.time;
           console.log(sendData);
           Dolphin.ajax({
               url: CTX + '/api/00923c89a610415d8e70b1b4f2380e75',
               type: 'put',
               data: Dolphin.json2string(sendData),
               forceUrl: 'true',
               onSuccess: function (data) {
                   $.toast("保存成功", function () {
                       location.reload();
                   });
               }
           });


   //        Dolphin.ajax({
   //            url:'/api/5cb78c416b674cab93f1c60f2c09d375',
   //            data: Dolphin.json2string(data),
   //            onSuccess: function (data) {
   //                location.reload();
   //            }
   //        })
       });*/
    $("#calendar").calendar();
</script>
</body>
</html>